package com.foreveross.bsl.mam.app.application.impl;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Named;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.mam.app.application.AppVersionWidgetVersionApplication;
import com.foreveross.bsl.mam.app.application.vo.AppVersionWidgetVersionVo;
import com.foreveross.bsl.mam.model.app.domain.AppVersionWidgetVersion;

/**
 * @author tanzhixiong
 * @2013-6-22 @上午11:54:34
 */
@Named("appVersionWidgetVersionApplication")
public class AppVersionWidgetVersionApplicationImpl implements
		AppVersionWidgetVersionApplication {

	public AppVersionWidgetVersionVo saveEntity(AppVersionWidgetVersionVo entity) {
		AppVersionWidgetVersion appVersionWidgetVersion = new AppVersionWidgetVersion();
		BeanHelper.copyProperties(appVersionWidgetVersion, entity);
		AppVersionWidgetVersion result = (AppVersionWidgetVersion) appVersionWidgetVersion
				.save();
		BeanHelper.copyProperties(entity, result);
		return entity;
	}

	@SuppressWarnings("unchecked")
	public List<AppVersionWidgetVersionVo> saveEntities(
			List<AppVersionWidgetVersionVo> entities) {
		// AppVersionWidgetVersion dbOperator = new AppVersionWidgetVersion();
		List<AppVersionWidgetVersion> relations = new ArrayList<AppVersionWidgetVersion>();
		for (int i = 0; i < entities.size(); i++) {
			AppVersionWidgetVersion appVersionWidgetVersion = new AppVersionWidgetVersion();
			AppVersionWidgetVersionVo vo = entities.get(i);
			BeanHelper.copyProperties(appVersionWidgetVersion, vo);
			List<AppVersionWidgetVersion> list = appVersionWidgetVersion
					.findListByProperty(
							new String[] { "appId", "platform", "appVersion",
									"widgetId", "widgetVersion" },
							new OperatEnum[] { OperatEnum.EQ, OperatEnum.EQ,
									OperatEnum.EQ, OperatEnum.EQ, OperatEnum.EQ },
							new String[] { vo.getAppId(), vo.getPlatform(),
									vo.getAppVersion(), vo.getWidgetId(),
									vo.getWidgetVersion() });
			if (list == null || list.size() == 0) {
				appVersionWidgetVersion.save();
				relations.add(appVersionWidgetVersion);
			}
		}
		return entities;
	}

	@Override
	public AppVersionWidgetVersionVo updateEntity(
			AppVersionWidgetVersionVo entity) {
		AppVersionWidgetVersion appVersionWidgetVersion = new AppVersionWidgetVersion();
		BeanHelper.copyProperties(appVersionWidgetVersion, entity);
		appVersionWidgetVersion.save();
		BeanHelper.copyProperties(entity, appVersionWidgetVersion);
		return entity;
	}

	@Override
	public void deleteEntity(String id) {
		AppVersionWidgetVersion appVersionWidgetVersion = new AppVersionWidgetVersion();
		appVersionWidgetVersion.delete(id);
	}

	@Override
	public AppVersionWidgetVersionVo getEntity(String id) {
		AppVersionWidgetVersion appVersionWidgetVersion = new AppVersionWidgetVersion();
		appVersionWidgetVersion = (AppVersionWidgetVersion) appVersionWidgetVersion
				.findOne(id);
		AppVersionWidgetVersionVo appVersionWidgetVersionVo = new AppVersionWidgetVersionVo();
		BeanHelper.copyProperties(appVersionWidgetVersionVo,
				appVersionWidgetVersion);
		return appVersionWidgetVersionVo;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppVersionWidgetVersionVo> getListByPlatEntity(String platform,
			String version, String appId) {
		AppVersionWidgetVersion appVersion = new AppVersionWidgetVersion();
		List<AppVersionWidgetVersion> appVersionWidgetVersionlist = appVersion
				.findListByProperty(new String[] { "appId", "platform",
						"appVersion" }, new OperatEnum[] { OperatEnum.EQ,
						OperatEnum.EQ, OperatEnum.EQ }, new String[] { appId,
						platform, version });
		List<AppVersionWidgetVersionVo> result = new ArrayList<AppVersionWidgetVersionVo>();
		if (appVersionWidgetVersionlist != null) {
			AppVersionWidgetVersionVo vo;
			for (int i = 0; i < appVersionWidgetVersionlist.size(); i++) {
				vo = new AppVersionWidgetVersionVo();
				BeanHelper.copyProperties(vo,
						appVersionWidgetVersionlist.get(i));
				result.add(vo);
			}
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public AppVersionWidgetVersionVo getAppWidgetEntity(String appId,
			String platform, String version, String widgetId) {
		AppVersionWidgetVersion appVersion = new AppVersionWidgetVersion();
		List<AppVersionWidgetVersion> appVersionWidgetVersionlist = appVersion
				.findListByProperty(new String[] { "appId", "platform",
						"appVersion", "widgetId" }, new OperatEnum[] {
						OperatEnum.EQ, OperatEnum.EQ, OperatEnum.EQ,
						OperatEnum.EQ }, new String[] { appId, platform,
						version, widgetId });
		if (appVersionWidgetVersionlist != null
				&& appVersionWidgetVersionlist.size() > 0) {
			AppVersionWidgetVersionVo vo = new AppVersionWidgetVersionVo();
			BeanHelper.copyProperties(vo, appVersionWidgetVersionlist.get(0));
			return vo;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppVersionWidgetVersionVo> getListByWidget(String widgetId,
			String version) {
		AppVersionWidgetVersion appVersion = new AppVersionWidgetVersion();
		List<AppVersionWidgetVersion> appVersionWidgetVersionlist = appVersion
				.findListByProperty(
						new String[] { "widgetId", "widgetVersion" },
						new OperatEnum[] { OperatEnum.EQ, OperatEnum.EQ },
						new String[] { widgetId, version });
		List<AppVersionWidgetVersionVo> result = new ArrayList<AppVersionWidgetVersionVo>();
		if (appVersionWidgetVersionlist != null) {
			AppVersionWidgetVersionVo vo;
			for (int i = 0; i < appVersionWidgetVersionlist.size(); i++) {
				vo = new AppVersionWidgetVersionVo();
				BeanHelper.copyProperties(vo,
						appVersionWidgetVersionlist.get(i));
				result.add(vo);
			}
		}
		return result;
	}

	@Override
	public void deleteWidget(String widgetId, String version) {
		// TODO Auto-generated method stub
		AppVersionWidgetVersion appVersion = new AppVersionWidgetVersion();
		appVersion.deleteByProperty(
				new String[] { "widgetId", "widgetVersion" }, new OperatEnum[] {
						OperatEnum.EQ, OperatEnum.EQ }, new String[] {
						widgetId, version });
	}

	@Override
	public void deleteByApp(String appId) {
		AppVersionWidgetVersion appVersion = new AppVersionWidgetVersion();
		appVersion.deleteByProperty("appId", OperatEnum.EQ, appId);
	}

	public void deleteByAppVersion(String platform, String version, String appId) {
		AppVersionWidgetVersion appVersion = new AppVersionWidgetVersion();
		appVersion.deleteByProperty(new String[] { "appId", "platform",
				"appVersion" }, new OperatEnum[] { OperatEnum.EQ,
				OperatEnum.EQ, OperatEnum.EQ }, new String[] { appId, platform,
				version });
	}

	@Override
	public void deleteEntities(List<AppVersionWidgetVersionVo> entities) {
		AppVersionWidgetVersion versionRelation = new AppVersionWidgetVersion();
		for (int i = 0; i < entities.size(); i++) {
			AppVersionWidgetVersionVo entity = entities.get(i);
			versionRelation.deleteByProperty(new String[] { "appId",
					"platform", "appVersion", "widgetId", "widgetVersion" },
					new OperatEnum[] { OperatEnum.EQ, OperatEnum.EQ,
							OperatEnum.EQ, OperatEnum.EQ, OperatEnum.EQ },
					new String[] { entity.getAppId(), entity.getPlatform(),
							entity.getAppVersion(), entity.getWidgetId(),
							entity.getWidgetVersion() });

		}
	}

	@Override
	public void deleteByWidgetRelation(String version, String appId) {
		// TODO Auto-generated method stub
		AppVersionWidgetVersion appVersion = new AppVersionWidgetVersion();
		appVersion.deleteByProperty(new String[] { "appId", "appVersion" },
				new OperatEnum[] { OperatEnum.EQ, OperatEnum.EQ },
				new String[] { appId, version });
	}
	
	@Override
	public void delByVersion(String platform, String appVersion, String appId,
			String widgetVersion, String widgetId) {
		AppVersionWidgetVersion appWidegetVersion = new AppVersionWidgetVersion();
		appWidegetVersion.deleteByProperty(new String[] { "platform","appVersion","appId", "widgetVersion","widgetId" },
				new OperatEnum[] { OperatEnum.EQ, OperatEnum.EQ,OperatEnum.EQ, OperatEnum.EQ,OperatEnum.EQ },
				new String[] { platform, appVersion,appId, widgetVersion,widgetId});		
	}
}
